package com.yanze.customviewdemo.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import androidx.annotation.Nullable;

public class TaijiView extends View {
    private static final String TAG = "yanze_taiji";

    private Paint whitePaint;
    private Paint blackPaint;
    private float degrees = 0;

    public TaijiView(Context context) {
        this(context, null);
    }

    public TaijiView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public TaijiView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        whitePaint = new Paint();
        whitePaint.setAntiAlias(true);
        whitePaint.setColor(Color.WHITE);
        whitePaint.setStyle(Paint.Style.FILL);

        blackPaint = new Paint(whitePaint);
        blackPaint.setColor(Color.BLACK);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        int width = getWidth();
        int height = getHeight();
        canvas.translate(width / 2, height / 2);
        canvas.drawColor(Color.GRAY);

        canvas.rotate(degrees);

        int radius = Math.min(width / 2, height / 2);
        RectF rectF = new RectF(-radius, -radius, radius, radius);
//        RectF rectF = new RectF(0, 0, 2*radio, 2*radio);
        canvas.drawArc(rectF, 90, 180, true, whitePaint);
        canvas.drawArc(rectF, -90, 180, true, blackPaint);

        //画两个小圆(小圆半径是大圆的一半) 鱼头
        int smallRadius = radius / 2;
        canvas.drawCircle(0, smallRadius, smallRadius, whitePaint);
        canvas.drawCircle(0, -smallRadius, smallRadius, blackPaint);

        //绘制两个更小的圆(半径是小圆的4分之一) 鱼眼
        canvas.drawCircle(0, -smallRadius, smallRadius / 4, whitePaint);
        canvas.drawCircle(0, smallRadius, smallRadius / 4, blackPaint);
    }

    public void setDegrees(float degrees) {
        this.degrees = degrees;
        Log.i(TAG, "setDegrees: " + this.degrees);
        invalidate();
    }
}
